﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;
using ElementumForever.Interfaces;

namespace ElementumForever
{
   public class PhysicalConnector : IUpdateHandler
    {

        public IShape Shape { get; private set; }
        public Body Body { get; private set; }
        public bool UpdatePosition { get; set; }
        public bool UpdateRotation { get; set; }
        protected float OffsetX;
        protected float OffsetY;

        public PhysicalConnector(IShape shape, Body body) {
            this.Shape = shape;
            shape.OriginX=shape.Width / 2;
            shape.OriginY= shape.Height / 2;
            this.Body = body;
            OffsetX = shape.Width / 2-shape.OriginX;
            OffsetY = shape.Height / 2-shape.OriginY;
            UpdatePosition = true;
            UpdateRotation = true;
        }

        public void OnUpdate(GameTime gameTime) {
            if (UpdatePosition)
            {
                Vector2 position = this.Body.GetPosition();
                //const float pixelToMeterRatio = PhysicalFactory.PixelToMeter;

                Vector2 pos = CoordinateHelper.FromPhysicalWorldToScene(position);
                this.Shape.X=pos.X - this.OffsetX;
                this.Shape.Y=pos.Y - this.OffsetY;
            }
            if (UpdateRotation)
            {
                float angle = this.Body.GetAngle();//radians
                this.Shape.Rotation = -angle;
            }
            //throw new NotImplementedException();
        }
    }
}
